package module.building.view.buildingListPanel
{
	import code.consts.FilterConst;
	
	import com.darwin.darwinIsometric.core.isoConst.IsoType;
	import com.darwin.darwinUI.assist.IGroupItem;
	import com.darwin.darwinUI.controls.Button;
	import com.darwin.darwinUI.controls.ITabPanel;
	import com.darwin.darwinUI.controls.Label;
	import com.darwin.darwinUI.controls.LabelButton;
	import com.darwin.darwinUI.core.UIComponent;
	import com.darwin.darwinUI.events.UIEvent;
	import com.darwin.darwinUI.general.TextFieldFactory;
	
	import flash.display.DisplayObject;
	import flash.events.Event;
	import flash.events.MouseEvent;
	
	import main.AppControllerList;
	
	import module.building.event.BuildEvent;
	import module.building.model.dataObj.BuildingObj;
	import module.main.view.ResourceConst;
	import module.scene.SceneController;
	import module.scene.consts.ActionType;
	import module.scene.model.dataObj.IsoDataObj;
	import module.scene.view.iso.BuildingIsoObject;

	/**
	 * 建造列表面板 
	 * @author hc
	 */
	public class BuildingListPanel extends UIComponent
	{
		private var _data:Array;
		private var closeBtn:Button;
		private var leftBtn:Button;
		private var rightBtn:Button;
		private var title:UIComponent;
		private var contentBg:UIComponent;
		private var frameBg2:UIComponent;
//		private var pageLabel:Label;
		private var buildChoosePanel:BuildChoosePanel
		
		/**
		 * 构造方法</br> 
		 * 点击主界面的建造按钮后弹出此面板
		 */
		public function BuildingListPanel(data:Array)
		{
			super();
			_data = data;
			initView();
			initData();
			addEventListener(Event.ADDED_TO_STAGE, onAdded);
		}
		
		private function initView():void
		{
//			backgroundAuto = true;
			backgroundSkin = ResourceConst.BUILD_IMAGE_FRAME_BG;
			
			closeBtn = new Button();
			addChild(closeBtn);
//			close.backgroundAuto = true;
			closeBtn.backgroundSkin = ResourceConst.BUILD_BUTTON_CLOSE;			
			
			leftBtn = new Button();
			addChild(leftBtn);
			leftBtn.backgroundSkin = ResourceConst.BUILD_ARROW_LEFT;
			
			rightBtn = new Button();
			addChild(rightBtn);
			rightBtn.backgroundSkin = ResourceConst.BUILD_ARROW_RIGHT;
						
			title = new UIComponent();
			title.backgroundSkin = ResourceConst.BUILD_IMAGE_FRAME_TITLE;	
			var titleLabel:Label = TextFieldFactory.getInstance().getLabel("buildInterface_title");//new Label("建造");
			titleLabel.backgroundAuto = true;
			title.addChild(titleLabel);
			addChild(title);
			titleLabel.x = 54.85;
			titleLabel.y = 10.35;
			titleLabel.textColor = 0xE6DFCC;
			
			contentBg = new UIComponent();
			contentBg.backgroundSkin = ResourceConst.BUILD_IMAGE_CONTENT_BG;
			addChild(contentBg);
			
			frameBg2 = new UIComponent();
			frameBg2.backgroundSkin = ResourceConst.BUILD_IMAGE_FRAME_BG2;
			addChild(frameBg2);
			
			buildChoosePanel = new BuildChoosePanel(_data);
			addChild(buildChoosePanel);
			
//			pageLabel = new Label("");
//			addChild(pageLabel);
		}
		
		private function initData():void
		{
			setView(this,0,0,645.85,518.9);
			this.backgroundAuto = false;
			
			setView(closeBtn,604.45,14);
			setView(leftBtn,274.4,477);
			setView(rightBtn,344.45,477);
			
			setView(title,227.95,-17);
			title.mouseEnabled = false;
			title.mouseChildren = false;
			
			setView(contentBg,20.45,81.55,438.9,398.35);
			setView(frameBg2,73.9,83.05,553.9,396.3);
			setView(buildChoosePanel,26.9,46.3);
		}
		
		private function addListener():void
		{
			closeBtn.addEventListener(MouseEvent.CLICK, onClickClose);
			leftBtn.addEventListener(MouseEvent.CLICK, onClickLeft);
			rightBtn.addEventListener(MouseEvent.CLICK, onClickRight);
			buildChoosePanel.list.addEventListener(UIEvent.LIST_ROLL_COMP, onRollComplete);
			buildChoosePanel.list.addEventListener(UIEvent.LISTITEM_CLICK, onClickItem);
		}
		
		private function setView(obj:UIComponent, x:Number=0, y:Number=0, w:Number=0, h:Number=0):void
		{
			obj.backgroundAuto = true;
			obj.x = x;
			obj.y = y;
			if(w!=0)
			{
				if(obj.background)
				{
					obj.background.width = w;					
				}
				obj.width = w;
			}
			if(h!=0)
			{
				if(obj.background)
				{
					obj.background.height = h;
				}
				obj.height = h;								
			}			
		}
		
		private function close():void
		{
			var event:BuildEvent = new BuildEvent(BuildEvent.BUILD_CLOSE);
			dispatchEvent(event);
//			removeAllChild();
//			if(parent)
//			{
//				parent.removeChild(this);
//			}
		}
		
		private function page(pagePluse:int):void
		{
			buildChoosePanel.list.rollToPage(buildChoosePanel.list.currentPage + pagePluse, 1);
//			refreshPageLabel();
		}
		
//		public function refreshPageLabel():void
//		{
//			if(buildTabPanel.list.pageLength > 0)
//			{
//				pageLabel.text = buildTabPanel.list.currentPage+1+"/"+buildTabPanel.list.pageLength;				
//			}
//			else
//			{
//				pageLabel.text = "";
//			}
//		}
		
		private function onAdded(e:Event):void
		{
			addListener();
			buildChoosePanel.refreshPanel();
		}
		
		private function onClickClose(e:MouseEvent):void
		{
			close();
		}
		private function onClickLeft(e:MouseEvent):void
		{
			if(buildChoosePanel.list.currentPage ==0){return;}
			page(-1);
		}
		private function onClickRight(e:MouseEvent):void
		{
			if(buildChoosePanel.list.currentPage == buildChoosePanel.list.pageLength-1){return;}
			page(1);
		}
		private function checkRollButton():void
		{
			if(buildChoosePanel.list.currentPage > 0)
			{
				leftBtn.filters = [];
			}else
			{
				leftBtn.filters = [FilterConst.GREY_FILTER];
			}
			
			if(buildChoosePanel.list.currentPage+1 < buildChoosePanel.list.pageLength)
			{
				rightBtn.filters = [];
			}else
			{
				rightBtn.filters = [FilterConst.GREY_FILTER];
			}
		}
		
		private function onRollComplete(e:UIEvent):void
		{
			buildChoosePanel.refreshPanel();
			checkRollButton();
		}
		private function onClickItem(e:UIEvent):void
		{
			var item:BuildListItem = e.data as BuildListItem;//这是点击的对象,向控制器抛事件
			
//			var iso:IsoObjectObj = new IsoObjectObj();
//			iso.serverId = item.data.build.serverId;
//			iso.size= item.data.build.size;
//			var build:BuildingIsoObject = new BuildingIsoObject(iso);
//			build.isoType = IsoType.OBJECT;
//			AppControllerList.getInstance().sceneController.addBuilding(build);		
//			close();
			
			var event:BuildEvent = new BuildEvent(BuildEvent.BUILD_LIST_ITEM_SELECT,item);
			dispatchEvent(event);
		}

		public function get data():Array
		{
			return _data;
		}

		public function set data(value:Array):void
		{
			_data = value;
			buildChoosePanel.dataArray = value;			
//			checkRollButton();
		}

	}
}